package com.wangwenjun.concurrency.chapter14.threadpool;

import java.io.IOException;
import java.util.concurrent.*;

/**
 * @Package: com.wangwenjun.concurrency.chapter14.threadpool
 * @ClassName: UseThreadPool
 * @Author: lujieni
 * @Description: 1
 * @Date: 2021-03-11 14:26
 * @Version: 1.0
 *
 * 4种拒绝策略
 * new ThreadPoolExecutor.AbortPolicy() //银行满了,还有人进来,不处理这个人,直接抛出异常
 * new ThreadPoolExecutor.CallerRunsPolicy() //哪来的去哪里
 * new ThreadPoolExecutor.DiscardPolicy() //队列满了,丢掉任务,不会抛出异常
 * new ThreadPoolExecutor.DiscardOldestPolicy() //队列满了尝试去和最早的竞争但不会抛出异常
 *
 * 1.最开始每来1个任务就会创建1个核心线程
 * 2.核心线程数到了之后新的任务会放到阻塞队列中
 * 3.阻塞队列满了之后会继续创建非核心线程,当前运行的线程数量超过maximumPoolSize,则交给RejectedExecutionHandler来处理任务
 *
 *
 *
 */
public class UseThreadPool3 {


    public static void main(String[] args) {


        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,
                4,
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(4),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());


        Future<Integer> submit = threadPoolExecutor.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                TimeUnit.SECONDS.sleep(1);
                int i = 5 / 0;
                return 1;
            }
        });

        try {
            Integer i = submit.get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            System.out.println("exception");
            throw new RuntimeException(e);
        }

    }

}